var selectedRow = '';
var def_editType = 'add';

$(function () {
    regValidate(["#_parentDeptDataForm"]);
    regValidate(["#parentDeptDataForm"]);
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            infoParentDeptPage();
        },
    }
});

function doRefresh() {
    selectedRow = '';
    var deptName = $('#deptName').val();
    var status = $('#status').val();
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'basicDeptInfo/pageFindParentDeptData?t=' + new Date().getTime(),
        query: {
            deptName : deptName,
            status : status
        },
    });
}

function getParams(params) {
    var deptName = $('#deptName').val();
    var status = $('#status').val();
    return {
        deptName : deptName,
        status : status,
        pageSize : params.pageSize,
        pageNo : params.pageNumber
    };
}

function openAddParentDeptPage() {
    def_editType = 'add';
    $('#modelTitle').html('新增一级科室');
    $('#modalDeptCode').val('');
    $('#modalDeptName').val('');
    $('#modalSortNum').val('');
    $('#modalRemark').val('');
    $('#editParentDeptModal').modal('show');
}

function openEditParentDeptPage() {
    def_editType = 'edit';
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("编辑"));
        return;
    }
    $('#modelTitle').html('编辑一级科室');
    $('#modalDeptCode').val(selectedRow.deptCode);
    $('#modalDeptName').val(selectedRow.deptName);
    $('#modalSortNum').val(selectedRow.sortNum);
    $('#modalRemark').val(selectedRow.remark);
    $('#editParentDeptModal').modal('show');
}

function infoParentDeptPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    $('#lab_deptCode').html(selectedRow.deptCode);
    $('#lab_deptName').html(selectedRow.deptName);
    $('#lab_sortNum').html(selectedRow.sortNum);
    $('#lab_remark').html(selectedRow.remark);
    $('#parentDeptInfoModal').modal('show');
}

function closeParentDeptInfoModal() {
    $('#parentDeptInfoModal').modal('hide');
    resetFormTip('#parentDeptDataForm1');
}

function closeEditParentDeptModal() {
    $('#editParentDeptModal').modal('hide');
    resetFormTip('#parentDeptDataForm');
    resetInputCss('#parentDeptDataForm');
}

function doSaveParentDept() {
    if(!$("#parentDeptDataForm").valid()) return;//校验不通过
    var deptCode = $.trim($('#modalDeptCode').val());
    var deptName = $.trim($('#modalDeptName').val());
    var sortNum = $.trim($('#modalSortNum').val());
    sortNum = sortNum ? sortNum : 999;
    var remark = $.trim($('#modalRemark').val());
    var postData = {
        deptCode : deptCode,
        deptName : deptName,
        sortNum : sortNum,
        remark : remark,
    };
    var url = basePath + 'basicDeptInfo/insertBasicDeptInfo';
    if (def_editType == 'edit') {
        url = basePath + 'basicDeptInfo/editBasicDeptInfo';
        postData.id = selectedRow.id;
    }
    $.ajax({
        type: 'post',
        url: url,
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success(common_save_success);
                closeEditParentDeptModal();
                doRefresh();
            }else if(data&&!data.isSuccess){
                if (data.errorCode == 'EXIST_CODE') {
                    toastr.error('此科室编码已存在');
                    return;
                }
                if (data.errorCode == 'EXIST') {
                    toastr.error('此科室名称已存在');
                    return;
                }
                toastr.error(data.errorDesc);
            }else{
                toastr.error(common_save_fail);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function checkParentDeptDelete() {
    if (!selectedRow.id) {
        toastr.error(common_no_choose_msg.format("删除"));
        return;
    }
    var postData = {
        parentCode : selectedRow.deptCode
    };
    $.ajax({
        type: 'post',
        url: basePath + 'basicDeptInfo/checkDeptDelete?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if (!data) {
                toastr.error('校验删除失败');
                return;
            }
            if (!data.isSuccess) {
                if (data.errorCode == 'HAS_CHILD') {
                    toastr.error('该科室下包含二级科室，无法删除');
                } else {
                    toastr.error(data.errorDesc);
                    return;
                }
            } else {
                var strHtml = '是否删除科室【'+selectedRow.deptName+'】？该操作会变更数据！';
                swalToastr('', strHtml, 'warning', deleteDept);
            }
            return;
        },
    });
}

function deleteDept() {
    var postData = {
        id : selectedRow.id,
        status : 'delete'
    };
    $.ajax({
        type: 'post',
        url: basePath + 'basicDeptInfo/changeState',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('删除成功');
                doRefresh();
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function changeParentDeptState() {
    if (!selectedRow.id) {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var status = 'disabled';
    var str = '冻结';
    if (selectedRow.status == 'disabled') {
        status = 'enabled';
        str = '恢复';
    }
    var postData = {
        id : selectedRow.id,
        status : status
    };
    $.ajax({
        type: 'post',
        url: basePath + 'basicDeptInfo/changeState',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success(str + '成功');
                doRefresh();
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error(str + '失败');
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });

}

function openChildDeptPage() {
    if (!selectedRow || selectedRow == '') {
        toastr.error(common_no_choose_msg.format("查看二级科室"));
        return;
    }
    if (!selectedRow.deptCode) {
        toastr.error("所选数据存在问题，请刷新页面重试");
        return;
    }
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'basicDeptInfo/childDeptPage?deptCode=' + selectedRow.deptCode + '&t=' + new Date().getTime()+ '&' +$('#searchForm').serialize() +
        '&name=' + encodeURI(encodeURI(selectedRow.deptName)) + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

//------------------------------------------------------------------------列表数据初始化
function formatStatus(value, row, index) {
    if (value == 'enabled') {
        return '有效';
    }
    if (value == 'disabled') {
        return '无效';
    }
    return value;
}

//左边选中事件
$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $("#dataTable .success").removeClass("success");
    $(ele).addClass("success");
    selectedRow = row;
    if (row.status == 'enabled') {
        $('#_btnChangeState').html('冻结');
    } else {
        $('#_btnChangeState').html('恢复');
    }
});
